package com.mashibing.servicemap.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mashibing.servicemap.dto.DicDistrict;
import com.mashibing.servicemap.mapper.DicDistrictMapper;
import com.mashibing.servicemap.remote.MapDicDistrictClient;
import com.mashibing.servicemap.service.DicDistrictService;
import lombok.extern.slf4j.Slf4j;
import org.mashibing.constant.AmapConfigConstant;
import org.mashibing.constant.CommonStatusEnum;
import org.mashibing.dto.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author:
 * @create: 2024/4/16 10:22
 * @description:
 */
@Service
@Slf4j
public class DicDistrictServiceImpl implements DicDistrictService {

    @Autowired
    private MapDicDistrictClient mapDicDistrictClient;

    @Autowired
    private DicDistrictMapper dicDistrictMapper;

    @Override
    public ResponseResult initDicDistrict(String keywords) {
        //TODO 此段代码有多个for循环嵌套，需优化

        // 拼接url
        String districtUrl = mapDicDistrictClient.dicDistrict(keywords);
        log.info("拼接后的URL: {}",districtUrl);
        // 解析json并入库
        JSONObject districtJsonObject = JSONObject.parseObject(districtUrl);
        if (!Objects.equals(districtJsonObject.get(AmapConfigConstant.STATUS),"1")) {
            return ResponseResult.fail(CommonStatusEnum.MAP_DISTRICT_ERROR.getCode(), CommonStatusEnum.MAP_DISTRICT_ERROR.getValue());
        }
        JSONArray districtsJsonArray = districtJsonObject.getJSONArray(AmapConfigConstant.DISTRICTS);
        JSONObject jsonObject = districtsJsonArray.getJSONObject(0);
        // 国家
        String adCode = jsonObject.getString(AmapConfigConstant.ADCODE);
        String name = jsonObject.getString(AmapConfigConstant.NAME);
        String parentAddressCode = "0";
        Integer level = generateLevel(jsonObject.getString(AmapConfigConstant.LEVEL));
        insertDicDistrict(adCode, name, parentAddressCode, level);
        // 省
        JSONArray provinceJsonArray = jsonObject.getJSONArray(AmapConfigConstant.DISTRICTS);
        for (int province = 0; province < provinceJsonArray.size(); province++) {
            JSONObject provinceObject = (JSONObject)JSONObject.toJSON(provinceJsonArray.get(province));
            String provinceAdCode = provinceObject.getString(AmapConfigConstant.ADCODE);
            String provinceName = provinceObject.getString(AmapConfigConstant.NAME);
            String provinceParentAddressCode = adCode;
            Integer provinceLevel = generateLevel(provinceObject.getString(AmapConfigConstant.LEVEL));
            insertDicDistrict(provinceAdCode, provinceName, provinceParentAddressCode, provinceLevel);
            // 市
            JSONArray cityJsonArray = provinceObject.getJSONArray(AmapConfigConstant.DISTRICTS);
            for (int city = 0; city < cityJsonArray.size(); city++) {
                JSONObject cityObject = (JSONObject)JSONObject.toJSON(cityJsonArray.get(city));
                String cityAdCode = cityObject.getString(AmapConfigConstant.ADCODE);
                String cityName = cityObject.getString(AmapConfigConstant.NAME);
                String cityParentAddressCode = provinceAdCode;
                Integer cityLevel = generateLevel(cityObject.getString(AmapConfigConstant.LEVEL));
                insertDicDistrict(cityAdCode, cityName, cityParentAddressCode, cityLevel);
                // 县
                JSONArray districtJsonArray = cityObject.getJSONArray(AmapConfigConstant.DISTRICTS);
                for (int district = 0; district < districtJsonArray.size(); district++) {
                    JSONObject districtObject = (JSONObject)JSONObject.toJSON(districtJsonArray.get(district));
                    // 只插入到县为止，街道暂过
                    if (Objects.equals(districtObject.getString(AmapConfigConstant.LEVEL),AmapConfigConstant.STREET)) {
                        continue;
                    }
                    String districtAdCode = districtObject.getString(AmapConfigConstant.ADCODE);
                    String districtName = districtObject.getString(AmapConfigConstant.NAME);
                    String districtParentAddressCode = cityAdCode;
                    Integer districtLevel = generateLevel(districtObject.getString(AmapConfigConstant.LEVEL));
                    insertDicDistrict(districtAdCode, districtName, districtParentAddressCode, districtLevel);
                }
            }
        }
        return ResponseResult.success();
    }

    private void insertDicDistrict(String addressCode, String addressName, String parentAddressCode, Integer level) {
        DicDistrict dicDistrict = new DicDistrict();
        dicDistrict.setAddressCode(addressCode);
        dicDistrict.setAddressName(addressName);
        dicDistrict.setParentAddressCode(parentAddressCode);
        dicDistrict.setLevel(level);
        dicDistrictMapper.insert(dicDistrict);
    }

    private int generateLevel(String level) {
        switch (level) {
            case "country":
                return 0;
            case "province":
                return 1;
            case "city":
                return 2;
            case "district":
                return 3;
            default:
                return -1;
        }
    }
}
